<?php
class KBEntrySender extends KBEntryModel
{
	
	function KBEntrySender() {
		
		parent::KBEntryModel;
				
		$this->controller = &new KBClientController;
		$this->controller->mod_rewrite = false;
	}
	
	
	
	function getSubscribersSql($category_id) {
		$sql = "
		SELECT 
			u.*
		FROM 
			{$this->tbl->data_to_user_rule} d, 
			{$this->tbl->data_to_user_value} dv,
			{$this->tbl->user}
		WHERE 1
			AND d.title = 'kb_category_to_admin'
			AND d.id = dv.rule_id
			AND dv.data_value IN ($category_id)
			AND dv.user_value = u.id";
			
		return $sql;
	}
	
	
	function &getCategories() { 
		$sql = "SELECT id, parent_id FROM {$this->tbl->category}";
		$result = &$this->db->Execute($sql) or die(db_error($sql));
		while($row = &$result->FetchRow()) {
			$data[$row['id']] = $row;
		}
		
		$data;
	}
	
	function &getParentCategories($id, $categories) {
		return TreeHelperUtil::getParentsById($categories, $id);
	}
	
	
	function getLetter($records) {
		
		$tpl = &new tplTemplatez(APP_EMAI_TMPL_DIR . 'updated_articles.html');
		
		
		foreach($records['posted'] as $k => $v) {		
			
			$v['article_link'] = $controller->getLink('entry', $v['category_id'], $v['id']);
			$v['body'] = $this->getSummary($v['body']);
			$v['date'] = strftime('%d %b, %Y', $v['posted_ts']); 
				
			$tpl->tplParse($v, 'row_posted');
		}
		
		$tpl->tplAssign($this->conf);
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}
	
	
	function getSummary($str, $num_signs = '150') {
	
		if($num_signs == 'all') { return $str; }
		
		$values = array('<br>', '<BR>',  '< /br>', '<li>', '<p>', '<P>', '  ');
		$str = strip_tags(str_replace($values, ' ', $str)); 
		
		if(strlen($str) < $num_signs) { return $str; }
		
		return substr($str, 0, strrpos(substr($str, 0, $num_signs), ' ')) . '...';
	}
	
	
	function send() {
	
	
	}
}
?>